#!/bin/ksh
umask 022
PATH=$PATH:/usr/bin:/bin:/etc:/usr/sbin; export PATH
## Working directory
WORKDIR=`dirname $0`
##
## BKSINST information
##
## Current ezlistings version
CURVER="5.0.14"
## Optional upgrade version
OPTVER=""
## Required upgrade version
REQVER="5.0.14"
## Product version number
VERSION=""
## Product name
PRODUCT="The Real Yellow Pages"

##
## Image directories for each book
##
BOOKTOTAL=1

TAFDIR="taf08"

TAFNM="Tampa, FL"

TAFYR="${TAFNM} (Mar 2008/2009)"

TAF="${TAFYR}\n${TAFDIR}"

BOOKS_DIR="${WORKDIR}/../../books"

BEEP="\007"

##
## Function used to build the _bksinst file and install each book
## that was selected.
##
function BuildBksInst {
	clear
	BooksDir=${NewDir}/books
	if [ ! -d ${BooksDir} ]; then
		mkdir ${NewDir}/books
	fi
	BKSINST=${BooksDir}/_bksinst.eza
	echo ${CURVER} > ${BKSINST}
	echo ${OPTVER} >> ${BKSINST}
	echo ${REQVER} >> ${BKSINST}
	## Beginning of tagged file
	## Two blank lines necessary for exe to read tagged file
	echo "" >> ${BKSINST}
	echo "" >> ${BKSINST}
	if [ "${DEFAULTBOOKCODE}" != "" ]; then
		echo "** DefaultBookCode **" >> ${BKSINST}
		echo "${DEFAULTBOOKCODE}" >> ${BKSINST}
	fi
	if [ ${Url} != "" ]; then
		echo "** URL **" >> ${BKSINST}
		echo "${Url}/books" >> ${BKSINST}
	fi
	echo "** Name **" >> ${BKSINST}
	echo ${PRODUCT} >> ${BKSINST}
	echo "** Books **" >> ${BKSINST}
	i=0
	## Loop through book array
	## Source directory for all Books is found one level above
	while [ i -lt ${cnt} ]; do
		case "${array[${i}]}"
		in
			1) echo ${TAF} >> ${BKSINST}
			   echo "Installing ${TAFYR} directory..."
			   cp -r ${BOOKS_DIR}/${TAFDIR} ${BooksDir}
			   ;;
		esac
		let "i = i + 1"
	done
	if [ ! -e ${BooksDir}/_bksinst.lic ]; then
	  echo  >> ${BooksDir}/_bksinst.lic
	fi
	for i in `find ${BooksDir} -name "xtr0*." -print | cut -f1 -d'.'`; do
		mv ${i}. ${i} > /dev/null 2>&1
	done
	cp ${WORKDIR}/*.exe ${NewDir}
	cp ${WORKDIR}/*.gif ${NewDir}
	cp ${WORKDIR}/*.txt ${NewDir}
	cp ${WORKDIR}/*.htm* ${NewDir}
	cp ${WORKDIR}/*.cab ${NewDir}
	echo "\n<tags><url>$url</url></tags>" >> ${NewDir}/client32.exe
	if [ -d ${WORKDIR}/../../mac ]; then
		if [ ! -d ${NewDir}/mac ]; then
			mkdir ${NewDir}/mac
		fi
		chmod 755 ${NewDir}/macintro.htm
		cp ${WORKDIR}/../../mac/*.* ${NewDir}/mac
		## START URL SUBSTITUTION
		Url=`echo "${Url}" |sed 's/\//\\\\\//g'`
		sed s/"<!-- \*\* WHITE PAGES URL \*\* -->"/"${Url}"/g  ${WORKDIR}/macintro.htm > ${NewDir}/macintro.htm
		## END OF URL SUBSTITUTION
	fi
}

##
## Function used to display the product license.  The user must accept this
## to continue.
##
function ShowLicense {
	cat ${WORKDIR}/license.txt | more
	echo "\nEnter \"Y\" to accept these terms and conditions ? (Y/N)\c "
	read ans
}

##
## Function used to prompt for the installation path.
##
function GetRootPath {
	clear
	echo "Please enter the full path to the ${PRODUCT} content directory."
	echo "This install will copy all files to this directory."
	echo
	echo "Install path: \c"
	read NewDir
}

##
## Function used to prompt for book selections.  The user may enter an
## individual number or a range of numbers.
##
function GetBooks {
	clear
	echo "Enter the number of the book or books you wish to install. You"
	echo "may comma separate each number or use a hyphen for consecutive"
	echo "numbers".
	echo
	echo "Example: 1,3,5-8 would install books 1,3 and 5 through 8"
	echo
	echo "\t 1) ${TAFNM}"
	echo
	echo "Enter choices: \c"
	read BookChoices
}

##
## Function used to prompt for the ezlistings URL.
##
function GetUrl {
	clear
	echo "Enter the intranet URL which will point to ${PRODUCT} content"
	echo "directory."
	echo
	echo "Example: http://www.server.com/~server"
	echo
	echo "\nEnter the URL used for ${PRODUCT}: \c"
	read Url
	Url=`echo ${Url} | sed "s/\/$//"`
	url=${Url}
}

##
## Function used to enter book choices into an array.  A book choice is
## compared to what is in the array already.  If it is a duplicate entry
## it is ignored, otherwise it is added to the array
##
function PutData {
	if [ ${Data} -ge 1 -a ${Data} -le ${BOOKTOTAL} ]; then
		i=0
		## Check each book entry to see if choice already exists
		while [ i -lt ${cnt} ]; do
			if [ "${array[${i}]}" = "${Data}" ]; then
				break;
			fi
			let "i = i + 1"
		done
		## If choice did not exist add choice to array
		if [ ${i} -eq ${cnt} ]; then
			array[${cnt}]="${Data}"
			let "cnt = cnt + 1"
		fi
	fi
}

##
## Function used to generate numbers for a range of book choces entered.
##
function PutRangeData {
	newData=`echo ${Data} | sed 's/-/ /g'`
	## Blank out delimeter and convert into command line entries
	set -- ${newData}
	j=$1
	while [ $j -le $2 ]; do
		Data=$j
		PutData
		let "j = j + 1"
	done
}

##
## Function used to parse the book choice string.  The function uses the "set"
## command to simulate command line entries.  It then shifts through entry to
## it reaches the end.  If a - is found, the entry is assumed to be a range of
## numbers.  Any number outside of 1 - 9 will be ignored.
##
function ParseCmd {
	newline=`echo ${BookChoices} | sed 's/,/ /g'`
	set -- ${newline}
	while [ $# -gt 0 ]; do
		Data=${1}
		if [ "`echo ${Data} | grep "-"`" = "" ]; then
			PutData
		else
			PutRangeData
		fi
		shift
	done
}

##
## Function used to display install information.  This function is called
## just before installation begins.  Last chance to abort.
##
function BeginInstall {
	clear
	echo "The following information will be used to install"
	echo "${PRODUCT} Server."
	echo
	echo "Target directory:\t\t${NewDir}"
	echo
	echo "URL pointing to ${PRODUCT} data:\t${Url}"
	echo
	echo "Default book:\t\t\t${DEFAULTBOOK}"
	echo
	echo "Books to install:"
	i=0
	## Loop through book array and display book choices by name
	while [ i -lt ${cnt} ]; do
		case "${array[${i}]}"
		in
			1) echo "\t${TAFYR}";;
		esac
		let "i = i + 1"
	done
	echo
	echo "Do you want to continue with the install? (Y/N)\c"
	read ans
}

##
## Function used to prompt the user for a default book
##
function GetDefaultBook {
	bookAns=1
	if [ ${cnt} -gt 1 ]; then
		while [ 1 ]; do
			clear
			i=0
			j=0
			echo "Select a default book to open when ${PRODUCT} application"
			echo "is started by an end user."
			echo
			while [ i -lt ${cnt} ]; do
				let "j = j + 1"
				case "${array[${i}]}"
				in
					1) echo "${j}) ${TAFYR}";;
				esac
				let "i = i + 1"
			done
			echo
			echo "Select a default book: (1 - ${cnt}) \c"
			read bookAns
			## Make sure default choice is within range
			if [ ${bookAns} -ge 1 -a ${bookAns} -le ${cnt} ]; then
				break
			fi
			echo ${BEEP}
		done
	fi
	let "bookAns = bookAns - 1"
	case "${array[${bookAns}]}"
	in
		1) DEFAULTBOOKCODE="taf";;
	esac
	case "${array[${bookAns}]}"
	in
		1) DEFAULTBOOK="${TAFNM}";;
	esac
}

##
## Main
##

## Global variables
##
## Answer to Yes or No question
ans=""
## Set to a book array value
Data=""
## Book choice array
array=
## Number of books selected
cnt=0
## Install directory
NewDir=""
## Web server root path
NewDir=""
## The product's root directory
Url=""
## Default book choice
DEFAULTBOOK=""
DEFAULTBOOKCODE=""

clear
ShowLicense
clear
if [ "${ans}" = "Y" -o "${ans}" = "y" ]; then
	GetRootPath
	GetUrl
	while [ 1 ]; do
		GetBooks
		ParseCmd
		if [ ${cnt} -eq 0 ]; then
			echo
			echo ${BEEP}
			echo "No books were selected!  Do you want to make a selection? (Y/N) \c"
			read ans
			if [ "${ans}" = "Y" -o "${ans}" = "y" ]; then
				continue
			fi
		fi
		break
	done
	#InstallPath
	GetDefaultBook

	BeginInstall
	if [ "${ans}" = "Y" -o "${ans}" = "y" ]; then
		if [ ! -d ${NewDir} ]; then
			mkdir -p ${NewDir}
		fi
		BuildBksInst
	fi
fi
exit 0

